package pers.course.week9;

import java.text.NumberFormat;
import java.util.Scanner;

class Account{
    double balance;
    Account(double init_balance){
        balance = init_balance;
    }
    public double getBalance(){
        return balance;
    }
    public boolean deposit(double amt){
        balance += amt;
        return true;
    }
    public boolean withdraw(double amt){
        if( balance < amt){
            return false;
        }
        else{
            balance -= amt;
            return true;
        }
    }
}

class SavingsAccount extends Account{
    SavingsAccount(double balanceInput, double interesOrProtectInput){
        super(balanceInput);
    }
}

class CheckingAccount extends Account{
    CheckingAccount(double balanceInput, double interesOrProtectInput){
        super(balanceInput);
    }
    CheckingAccount(double balanceInput){
        super(balanceInput);
    }
}

class Bank {
    Customer[] customers;
    int numberOfCustomer;
    Bank(){
        customers = new Customer[20];
        numberOfCustomer = 0;
    }
    void addCustomer(String firstName, String lastName){
        customers[numberOfCustomer] = new Customer(firstName, lastName);
        numberOfCustomer++;
    }
    Customer getCustomer(int index){
        return customers[index];
    }
    int getNumOfCustomers(){
        return numberOfCustomer;
    }
}

public class HeterogeneousCollection{
    public static void main(String[] args) {
        Bank bank = new Bank();
        NumberFormat currency_format = NumberFormat.getCurrencyInstance();
        Customer customer;
        int curCustomer = 0;
        Scanner s = new Scanner(System.in);
        int t = s.nextInt();
        s.nextLine();
        // Create several customers and their accounts according to data
        while (t-- > 0) {
            String f = s.next();
            String l = s.next();
            s.nextLine();
            bank.addCustomer(f, l);
            customer = bank.getCustomer(curCustomer++);
            int numAccount = s.nextInt();
            s.nextLine();
            while (numAccount-- > 0) {
                String[] type = s.nextLine().split(" ");
                double balance;
                double interesOrProtect;
                //System.out.println(type[0]);
                if (type[0].equals("C")) {
                    //System.out.println("c");
                    balance = Double.parseDouble(type[1]);
                    if (type.length == 3) {
                        interesOrProtect = Double.parseDouble(type[2]);
                        customer.addAccount(new CheckingAccount(balance, interesOrProtect));
                    } else {
                        customer.addAccount(new CheckingAccount(balance));
                        //interesOrProtect = Double.parseDouble(type[2]);
                    }
                } else if (type[0].equals("S")) {
                    //System.out.println("s");
                    balance = Double.parseDouble(type[1]);
                    interesOrProtect = Double.parseDouble(type[2]);
                    customer.addAccount(new SavingsAccount(balance, interesOrProtect));
                } else if (type[0].equals("A")) {
                    //System.out.println("a");
                    int cIndex = Integer.parseInt(type[1]);
                    int aIndex = Integer.parseInt(type[2]);
                    customer.addAccount(bank.getCustomer(cIndex).getAccount(aIndex));
                }
            }
        }
// Generate a report
        System.out.println("CUSTOMERS REPORT");
        System.out.println("================");
        for ( int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++ ) {
            customer = bank.getCustomer(cust_idx);
            System.out.println();
            System.out.println("Customer: " + customer.getLastName() + ", " + customer.getFirstName());
            //System.out.println(customer.getNumOfAccounts());
            for ( int acct_idx = 0; acct_idx < customer.getNumOfAccounts(); acct_idx++ ) {
                Account account = customer.getAccount(acct_idx);
                String account_type = "";
// Determine the account type
/*** Step 1:
 **** Use the instanceof operator to test what type of account
 **** we have and set account_type to an appropriate value, such
 **** as "Savings Account" or "Checking Account".
 ***/
                if(account instanceof SavingsAccount){
                    System.out.println("    Savings Account" + currency_format.format(account.balance));
                }else{
                    System.out.println("    Checking Account" + currency_format.format(account.balance));
                }
// Print the current balance of the account
/*** Step 2:
 **** Print out the type of account and the balance.
 **** Feel free to use the currency_format formatter
 **** to generate a "currency string" for the balance.
 ***/
            }
        }
    }
}

class Customer{
    String firstName;
    String lastName;
    Account[] account;
    int numberOfAccounts;
    int getNumOfAccounts(){
        return numberOfAccounts;
    }
    String getLastName(){
        return lastName;
    }
    String	getFirstName(){
        return firstName;
    }
    Account getAccount(int index){
        return account[index];
    }
    void addAccount(Account accountInput){
        if(accountInput instanceof CheckingAccount){
            this.account[numberOfAccounts] = (CheckingAccount)accountInput;
        }else{
            this.account[numberOfAccounts] = (SavingsAccount)accountInput;
        }
        //System.out.println(numberOfAccounts);
        numberOfAccounts++;
    }
    Customer(){
        numberOfAccounts = 0;
    }
    Customer(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
        account = new Account[2];
        numberOfAccounts = 0;
    }
}
